package com.zhangzhan.gulimallmember.controller;

import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.R;
import com.zhangzhan.gulimallmember.entity.MemberEntity;
import com.zhangzhan.gulimallmember.service.MemberService;
import com.zhangzhan.gulimallmember.tos.GiteeUserInfoTo;
import com.zhangzhan.gulimallmember.tos.UserLoginTo;
import com.zhangzhan.gulimallmember.tos.UserRegisterTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

import static com.zhangzhan.gulimallcommcon.exception.exception_enum.ExceptionEnum.VALID_LOGIN_USER_EXCEPTION;
import static com.zhangzhan.gulimallcommcon.exception.exception_enum.ExceptionEnum.VALID_OAUTH2_USER_EXCEPTION;


/**
 * 会员
 *
 * @author zhangzhan
 * @email sunlightcs@gmail.com
 * @date 2024-06-12 18:17:09
 */
@RestController
@RequestMapping("/gulimallmember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 远程调用测试
     */
    @RequestMapping(value = "/member", method = RequestMethod.GET)
    public R feignMember() {
        MemberEntity memberDao = new MemberEntity();
        memberDao.setUsername("zhangsan");
        return R.ok().put("memberName", memberDao.getUsername());
    }


    /**
     * 注册保存
     */
    @RequestMapping(value = "/enroll", method = RequestMethod.POST)
    public R enrollUser(@RequestBody UserRegisterTo to) {
        R r = memberService.enrollUser(to);
        return r;
    }

    /**
     * gitee社交注册保存
     */
    @RequestMapping(value = "/oauth2/enroll", method = RequestMethod.POST)
    public R oauth2GiteeEnrollUser(@RequestBody GiteeUserInfoTo to) {
        MemberEntity member = memberService.oauth2GiteeEnrollUser(to);

        if (Objects.isNull(member)) {
            return R.error(VALID_OAUTH2_USER_EXCEPTION.getCode(), VALID_OAUTH2_USER_EXCEPTION.getMeg());
        }


        return R.ok().put("member", member);
    }

    /**
     * 登录校验
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public R login(@RequestBody UserLoginTo to) {
        MemberEntity member = memberService.login(to);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        if (Objects.isNull(member) || !bCryptPasswordEncoder.matches(to.getPassword(), member.getPassword())) {
            return R.error(VALID_LOGIN_USER_EXCEPTION.getCode(), VALID_LOGIN_USER_EXCEPTION.getMeg());
        }
        return R.ok().put("data", member);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimallmember:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gulimallmember:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimallmember:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallmember:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimallmember:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
